﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;
namespace DataAccess_Layer
{
  public class GiangHangDAC

    {
        public static DataClasses_RetailerDataContext context = DataAccessHelper.DB;
        public static List<GiangHangDTO> getALL()
        {
            List<GiangHangDTO> cats = null;

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            try
            {
                helper = new DataAccessHelper();

                rdr = helper.excuteSP("SP_Get_All_GiangHangSP");

                if (rdr.HasRows)
                {
                    cats = new List<GiangHangDTO>();
                    while (rdr.Read())
                    {
                        GiangHangDTO cat = createGiangHang(ref rdr);
                        cats.Add(cat);
                    }
                }
            }
            finally
            {
                helper.close();
                rdr.Close();
            }

            return cats;
        }
        private static GiangHangDTO createGiangHang(ref SqlDataReader rdr)
        {
            GiangHangDTO cat = new GiangHangDTO();
            cat.ID = (long)rdr["ID"];
            cat.TenSP = (string)rdr["Ten"];
            cat.MaCL = (long)rdr["MaChungLoaiSP"];
            cat.Hinh = (string)rdr["Hinh"];
            return cat;
        }

        public static void Insert(GiangHangDTO gh)
        {
            NCC_GiangHang ghncc = new NCC_GiangHang();
            ghncc.ID = gh.ID;
            ghncc.Hinh = gh.Hinh;
            ghncc.Ten = gh.TenSP;
            ghncc.MaChungLoaiSP = (int)gh.MaCL;
            ghncc.MoTa = gh.MoTa;
            ghncc.Gia = gh.Gia;
            ghncc.NCC_UN = gh.UserNCC;
            ghncc.SLToiThieu = gh.SoLuong;
            ghncc.NgayCapNhatSaucung = gh.NgayCapNhat;
            context.NCC_GiangHangs.InsertOnSubmit(ghncc);
            DataAccessHelper.Save();
        }
    
    }
}
